﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :文件菜单操作.菜单权限分配设置.以及提取菜单树操作.
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;
using Hisun.Models;

namespace Hisun.Controllers
{
    public class AbcMenu
    {
        private string lastError;
        private HisunDBDataContext db = new HisunDBDataContext();

        public string LastError
        {
            get { return lastError; }
            private set { lastError = value; }
        }

        /// <summary>
        /// 修改菜单项
        /// </summary>
        /// <param name="menuID">菜单ID</param>
        /// <param name="menuname">菜单名称</param>
        /// <param name="pageFile">页面文件</param>
        /// <param name="isLastNode">是否末级</param>
        /// <param name="partentID">父节点</param>
        /// <param name="roleID">权限ID</param>
        /// <returns></returns>
        public int ModifyMenu(int menuID, string menuname, string pageFile, bool isLastNode, int partentID, int roleID)
        {
            try
            {
                var menu = (from m in db.TAbc_JS_Menus
                            where m.菜单ID == menuID
                            select m).Single();
                menu.菜单名称 = menuname;
                menu.父ID = partentID;
                menu.权限ID = roleID;
                menu.是否末级 = isLastNode;
                menu.页面文件 = pageFile;
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuname">菜单名称</param>
        /// <param name="pageFile">页面文件</param>
        /// <param name="isLastNode">是否末级</param>
        /// <param name="partentID">父节点</param>
        /// <param name="roleID">权限ID</param>
        /// <returns></returns>
        public int AddMenu(string menuname, string pageFile, bool isLastNode, int partentID, int roleID)
        {
            try
            {
                TAbc_JS_Menu menu = new TAbc_JS_Menu();
                menu.菜单名称 = menuname;
                menu.父ID = partentID;
                menu.是否末级 = isLastNode;
                menu.页面文件 = pageFile;
                menu.权限ID = roleID;
                db.TAbc_JS_Menus.InsertOnSubmit(menu);
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 删除菜单项
        /// </summary>
        /// <param name="menuID">菜单ID</param>
        /// <returns></returns>
        public int DeleteMenu(int menuID)
        {
            if (db.TAbc_JS_Menus.Count(m => m.菜单ID == menuID) == 0)
            {
                lastError = "没有该菜单项!";
                return -1;
            }
            var menu = (from m in db.TAbc_JS_Menus
                        where m.菜单ID == menuID
                        select m).Single();
            db.TAbc_JS_Menus.DeleteOnSubmit(menu);
            db.SubmitChanges(ConflictMode.ContinueOnConflict);
            return 0;
        }

        /// <summary>
        /// 返回 可以显示的并拥有权限的菜单
        /// </summary>
        /// <param name="auths"></param>
        /// <returns></returns>
        public List<TAbc_JS_Menu> GetInAuthsMenus(List<int> auths)
        {
            List<TAbc_JS_Menu> retval;
            var menus = (from m in db.TAbc_JS_Menus
                         where m.是否显示 == true
                         select m);
            retval = (from m in menus
                      where m.权限ID == 0 || auths.Contains(m.权限ID)
                      orderby m.父ID
                      select m).ToList();
            return retval;
        }


        private void FillData2TreeView(int menuID, IEnumerable<TAbc_JS_Menu> TotalMenus, ref TreeNode tn)
        {
            var menus = (from m in TotalMenus
                         where m.父ID == menuID
                         orderby m.顺序ID descending
                         select m);
            if (menus.Count() == 0)
                return;
            foreach (var menu in menus)
            {
                if (menu.是否末级)
                {
                 //   tn.ChildNodes.Add(new TreeNode(@"&nbsp" + menu.菜单名称, menu.菜单ID.ToString(), @"/App_Themes/2011_Blue/Images/menu_botton.gif", menu.页面文件, "main"));
                    tn.ChildNodes.Add(new TreeNode(@"&nbsp" + menu.菜单名称, menu.菜单ID.ToString(), @"/App_Themes/2011_Blue/Images/menu_botton.gif", "javascript:top.myTab.Cts('" + menu.菜单名称 + "','" + menu.页面文件 + "')", null));
                }
                else
                {
                    TreeNode childTN = new TreeNode(menu.菜单名称);
                    FillData2TreeView(menu.菜单ID, TotalMenus, ref childTN);
                    tn.ChildNodes.Add(childTN);
                }

            }
            
        }

        /// <summary>
        /// 获得当前帐号可以操作的菜单
        /// </summary>
        /// <param name="auths"></param>
        /// <returns></returns>
        public TreeNode GetOwnLeftMenu(List<int> auths)
        {
            var menus = GetInAuthsMenus(auths);
            TreeNode tn = new TreeNode("Hisun System");
            FillData2TreeView(0, menus, ref tn);
            return tn;
        }



    }
}
